# Install kubernetes

#VPS_PROXY='socks5://192.168.2.3:1081'
VPS_PROXY=''
VPS_PROXY_NULL=""

##########################################
# 5 kubeadm init
##########################################

# Get the IP address that VirtualBox has given this VM
#IPADDR=`ifconfig eth1 | grep Mask | awk '{print $2}'| cut -f2 -d:`
#IPADDR=`ifconfig enp0s8 | grep Mask | awk '{print $2}'| cut -f2 -d:`

#IPADDR="192.168.33.10"
IPADDR="$1"

if  [ $# != 1 ]; then
	IPADDR="127.0.0.1"
fi
echo "This VM has IP address: $IPADDR"
 
export ALL_PROXY=${VPS_PROXY}

# Set up Kubernetes
NODENAME=$(hostname -s)
echo "This VM has IP hostname: $IPADDR"
## kubeadm init --apiserver-cert-extra-sans=$IPADDR  --node-name $N#ODENAME

echo ""
echo "KUBEADM INIT ........"
echo "apiserver-advertise-address: $IPADD"R
echo ""

#kubeadm init --kubernetes-version=1.11.0 --apiserver-cert-extra-sans=$IPADDR  --node-name $NODENAME
sudo kubeadm init --kubernetes-version=1.11.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=$IPADDR \
--node-name=$NODENAME

export ALL_PROXY=${VPS_PROXY_NULL}
# Set up admin creds for the vagrant user
echo Copying credentials to /home/vagrant...
sudo --user=vagrant mkdir -p /home/vagrant/.kube
if [ -f /home/vagrant/.kube/config ]; then
	rm -rf /home/vagrant/.kube/config
fi
sudo cp -if /etc/kubernetes/admin.conf /home/vagrant/.kube/config
sudo chown $(id -u vagrant):$(id -g vagrant) /home/vagrant/.kube/config

#sudo tee /root/.bash_aliases <<-"EOF"
#export KUBECONFIG=/etc/kubernetes/admin.conf
#EOF
sudo tee -a /root/.bashrc <<-"EOF"
export KUBECONFIG=/etc/kubernetes/admin.conf
EOF

echo ""
echo "KUBECTL APPLY ........"
echo ""

export ALL_PROXY=${VPS_PROXY}
################################
sudo pkill -e -9 kubectl
sudo --user=vagrant kubectl proxy --port=8080 &
sleep 0.2
################################
#sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
sudo kubectl apply -f /vagrant/kube-flannel.yml

echo ""
echo "KUBECTL TAINT ........"
echo ""

# Allow schedule pods on the master
sudo kubectl taint nodes --all node-role.kubernetes.io/master-

# sudo kubeadm reset
